package com.hejl.config;

import com.hejl.function.RecruitServiceFunction;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author Hejl
 * @version 1.0
 * @description
 * @className RagConfig
 * @date 2025/6/11 10:50
 */
@Configuration
public class RagConfig {


    @Bean
    VectorStore vectorStore(EmbeddingModel embeddingModel) {
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(embeddingModel).build();
        //提前resources文件夹内所有后缀名为.txt文本内容
        String filePath = "何家乐简历.txt";
        //读取filePath下的所有txt文件

        TextReader textReader = new TextReader(filePath);//创建TextReader对象
        Map<String, Object> customMetadata = textReader.getCustomMetadata();//获取自定义元数据
        customMetadata.put("filePath", filePath);//设置自定义元数据
        List<Document> documents = textReader.get();
        //2 文本切分段落
        TokenTextSplitter splitter =
                new TokenTextSplitter(1200,
                        350, 5,
                        100, true);
        splitter.apply(documents);
        //3 向量化存储
        simpleVectorStore.add(documents);
        return simpleVectorStore;
    }
    @Bean
    @Description("某某是否有资格面试")
    public Function<RecruitServiceFunction.Request, RecruitServiceFunction.Response> recruitServiceFunction(){
        return new RecruitServiceFunction();
    }
}